{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "237cdf5f",
   "metadata": {},
   "source": [
    "## [dict](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "320dff07",
   "metadata": {},
   "source": [
    "* 练习\n",
    "```\n",
    ">>> tel = {'jack': 4098, 'sape': 4139}\n",
    ">>> tel['guido'] = 4127\n",
    ">>> tel\n",
    "{'jack': 4098, 'sape': 4139, 'guido': 4127}\n",
    ">>> tel['jack']\n",
    "4098\n",
    ">>> del tel['sape']\n",
    ">>> tel['irv'] = 4127\n",
    ">>> tel\n",
    "{'jack': 4098, 'guido': 4127, 'irv': 4127}\n",
    ">>> list(tel)\n",
    "['jack', 'guido', 'irv']\n",
    ">>> sorted(tel)\n",
    "['guido', 'irv', 'jack']\n",
    ">>> 'guido' in tel\n",
    "True\n",
    ">>> 'jack' not in tel\n",
    "False\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92d6ea4c",
   "metadata": {},
   "source": [
    "## 字典的查询\n",
    "> 1.in 方法查询\n",
    "> \n",
    "> 2.get 方法获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3d4e2e05",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'zhiyu', 'stu1802': 'yyk', 'stu1803': 'lj'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"zhiyu\",\n",
    "    \"stu1802\":\"yyk\",\n",
    "    \"stu1803\":\"lj\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4ab3af31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 key 是否再字典当中 用 in \n",
    "\"stu1801\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7e8e7a99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhiyu'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get 方法 获取字典中的值（ key 对应的value内容）\n",
    "info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "29a7d0fb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhiyu\n"
     ]
    }
   ],
   "source": [
    "# 综上所述：字典的查询方法一般步骤如下：\n",
    "if \"stu1801\" in info:\n",
    "    print(info.get(\"stu1801\"))\n",
    "else:\n",
    "    print(\"您查询的内容不存在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b67c8c4",
   "metadata": {},
   "source": [
    "### 字典的删除\n",
    "> 1.pop() 指定值删除\n",
    ">\n",
    "> 2.popitem() 随机删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "579da169",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'zhiyu', 'stu1802': 'yyk', 'stu1803': 'lj'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c6702b67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'yyk'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.pop('stu1802')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ea05f520",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'zhiyu', 'stu1803': 'lj'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "028f1ac3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1803', 'lj')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f75b9dca",
   "metadata": {},
   "source": [
    "## 字典的一般方法\n",
    "\n",
    "> 1.字典的循环遍历\n",
    ">\n",
    "> 2.fromkeys()方法\n",
    ">\n",
    "> 3.setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e4384494",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'zhiyu', 'stu1802': 'yyk', 'stu1803': 'lj'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"zhiyu\",\n",
    "    \"stu1802\":\"yyk\",\n",
    "    \"stu1803\":\"lj\"\n",
    "}\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f5eb5de9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1801 zhiyu\n",
      "1802 yyk\n",
      "1803 lj\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历字典\n",
    "# 循环遍历的最大用途就是：给所有的值使用同样的 操作/方法\n",
    "\n",
    "stu_id_list = []\n",
    "for k,v in info.items():\n",
    "    print(k[3:],v)\n",
    "    stu_id_list.append(k[3:])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2061172e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1801', '1802', '1803']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "332c2109",
   "metadata": {},
   "source": [
    "fromkeys()方法\n",
    "\n",
    "Python 字典 fromkeys() 函数用于创建一个新字典，以序列 seq 中元素做字典的键，value 为字典所有键对应的初始值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0682e0d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f90a35e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud1101': 101010, 'stud1102': 101010, 'stud1103': 101010}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化统一值的字典信息\n",
    "学生初始密码={}\n",
    "学生初始密码.fromkeys([\"stud1101\",\"stud1102\",\"stud1103\"],101010)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c25e9365",
   "metadata": {},
   "source": [
    "setdefault()方法\n",
    "\n",
    "字典setdefault() 方法和get()方法类似,返回指定键的值,如果键不在字典中,将会添加键并将值设置为一个指定值,默认为None。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d6541184",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a7476814",
   "metadata": {},
   "outputs": [],
   "source": [
    "info.setdefault('stu1804')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3ee3a702",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhichao'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu1805','zhichao')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d36f7c01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'zhiyu',\n",
       " 'stu1802': 'yyk',\n",
       " 'stu1803': 'lj',\n",
       " 'stu1804': None,\n",
       " 'stu1805': 'zhichao'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "334cad80",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
